#!/usr/bin/perl -w
#
use strict;

my $include = "include/";
my $srcdir  = "src/";
foreach (@ARGV)
{
    open FILE_H, "<$include$_";
    (my $fileheader) = split /\./, $_;
    open FILE_CPP, ">>$srcdir$fileheader.cpp";

    my $inclass = 0;
    my $className;
    my $completeLine = "";
    my $mutilLine = 0;
    while (my $line = <FILE_H>)
    {
        if ($line =~ m/class (\w+)$/ || $line =~ m/class (\w+) :/)
        {
            $inclass = 1;
            $className = $1;
        }

        $inclass = 0 if $line =~ /\}\;/;
        if ($inclass)
        {
            if ($line !~ /^\s*$/
                && $line !~ /^\s*\/\// 
                && $line !~ /^\s*\/\*/
                && $line !~ /^\s*\*/
                && $line !~ /$className/ 
                && $line !~ /private/ 
                && $line !~ /public/ 
                && $line !~ /protected/ 
                && $line !~ /friend/)
            {
                chomp ($line);
                if ($line !~ /\(.*\)/)
                {
                    if ($line =~ /\(/ && $line !~ /\)/)
                    {
                        $completeLine = $line;
                        $mutilLine = 1;
                        next;
                    }elsif ($line !~ /\(/ && $line !~ /\)/ && $mutilLine)
                    {
                        $line =~ s/^\s*//g;
                        $line =~ s/\s*$//g;
                        $completeLine .= $line;
                        next;
                    }elsif ($line !~ /\(/ && $line =~ /\)/ && $mutilLine)
                    {
                        $line =~ s/^\s*//g;
                        $line =~ s/\s*$//g;
                        $completeLine .= $line;
                        $mutilLine = 0;
                    }else
                    {
                        next;
                    }
                }else
                {
                    $completeLine = $line;
                }
                # inline 
                next if $completeLine =~ /inline/;
                # abstract func
                next if $completeLine =~ /.*=\s*0\s*;\s*$/;
                #
                $completeLine =~ s/^\s*//g;
                $completeLine =~ s/\s*$//g;
                (my $beforeParam, my $params, my $behindParam) = split /[()]/, $completeLine;

                (my $mem1, my $mem2, my $mem3, my $mem4) = split / /, $beforeParam;
                #   static   retval   pointer*  funcname
                #  virtual
                if ($mem1 eq "static")
                {
                    $mem1 = $mem2;
                    $mem2 = $mem3;
                    $mem3 = $mem4;
                }elsif ($mem1 eq "virtual")
                {
                    $mem1 = $mem2;
                    $mem2 = $mem3;
                    $mem3 = $mem4;
                }
                if ($mem2 eq "*")
                {
                    $mem1 .= "*";
                    $mem2 = $mem3;
                }
                #check const

                my $const = "";
                $const = "const" if $behindParam =~ /const\s*;\s*$/;

                #get params
                #$mutilLine =~ m/.*?(\(.*\))/;

                $params = "(" . $params . ")";
                $params =~ s#( = [-\w]*)#/\*$1\*/#g;
                #format 
                $params =~ s/,(\w)/, $1/g;
                #
                #$retType =~ tr/\t //d;
                print FILE_CPP "$mem1 " . "$className" . "::" . "$mem2" . " $params" . " $const" . "\n";
                print FILE_CPP "{\n    return ;\n}\n";
            }
        }
    }
}
